require 'rexml/document'
require 'family_tree_api/person/assertions/base'
module FamilyTreeApi
  module Person
    module Assertion
      
      class Name < Base
        attr_accessor :type, :title, :forms
        def initialize
          super
          @forms = Array.new
        end
        
        def full_text=(name)
          form = Name::Form.new
          form.full_text = name
          @forms << form
        end
        
        def full_text
          @forms.first.full_text or ''
        end
        
        def set_pieces(pieces)
          form = Name::Form.new
          given_pieces = pieces[:given].split(' ') if pieces[:given]
          family_pieces = pieces[:family].split(' ') if pieces[:family]
          given_pieces.each do |p|
            piece = Name::Form::Piece.new
            piece.type = 'Given'
            piece.value = p
            form.pieces << piece
          end
          family_pieces.each do |p|
            piece = Name::Form::Piece.new
            piece.type = 'Family'
            piece.value = p
            form.pieces << piece
          end
          form.full_text = form.pieces.collect{ |p| p.value }.join(' ')
          @forms << form
        end
        
        def to_xml
          e = super
          e.add_attribute('type', @type) if @type
          forms = e.add_element("forms")
          @forms.each { |f| forms.add_element(f.to_xml) }
          e
          # e.set_attribute('version',)
        end

        def self.parse(name_element)
          name = Name.new
          name.parse_attributes(name_element)
          name.type = name_element.attributes["type"]
          name_element.children.each do |e|
            name.forms = Name::Form.parse(e) if e.name == 'forms'
          end
          name
        end

      end
      
      class Name::Form
        attr_accessor :full_text, :pieces
        
        def initialize
          @full_text = ''
          @pieces = []
        end
        
        def self.parse(forms_element)
          unless forms_element.nil?
            forms = Array.new
            forms_element.each do |e|
              if e.name == 'form'
                form = Name::Form.new
                e.children.each do |f|
                  case f.name
                  when 'fullText'
                    form.full_text = f.content
                  when 'pieces'
                    form.pieces = Name::Form::Piece.parse(f)
                  end
                end
                forms << form
              end  
            end
            forms
          end
        end
        
        def to_xml
          e = REXML::Element.new('form')
          e.add_element("fullText").add_text(@full_text)
          if @pieces && @pieces.size > 0
            pieces = e.add_element('pieces')
            @pieces.each { |p| pieces.add_element(p.to_xml) }
          end
          e
        end
      end

      class Name::Form::Piece
        attr_accessor :type, :value

        def self.parse(pieces_element)
          unless pieces_element.nil?
            pieces = Array.new
            pieces_element.each do |e|
              if e.name == 'piece'
                piece = Name::Form::Piece.new
                piece.type = e.attributes['type']
                e.children.each do |v|
                  piece.value = v.content if v.name == 'value'
                end
                pieces << piece
              end
            end
            pieces
          end
        end
        
        def to_xml
          e = REXML::Element.new('piece')
          e.add_attribute('type',@type) if @type
          e.add_element('value').add_text(@value) if @value
          e
        end
      end
      
    end
  end
end